package com.dog.basics.api.controller.mtkj;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.api.mtkj.ApiEndOrderAddDto;
import com.dog.basics.bean.dto.api.mtkj.ApiGOrderAddDto;
import com.dog.basics.bean.dto.api.mtkj.ApiLyOrderAddDto;
import com.dog.basics.bean.dto.api.mtkj.ApiOrderFromDto;
import com.dog.basics.bean.vo.api.mtkj.ApiOrderPrepayVo;
import com.dog.basics.bean.vo.api.mtkj.ApiOrderVo;
import com.dog.basics.common.annotation.Limit;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.service.common.WeChatMiniService;
import com.dog.basics.service.mtkj.IMtDeviceOrderService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * <p>
 * 设备订单 前端控制器
 * </p>
 *
 * @author wph
 * @since 2020-09-29
 */
@RestController
@RequestMapping("/api/device/order")
@Api(value = "设备订单模块", tags = SwaggerConstants.API_ALL)
@AllArgsConstructor
public class MtDeviceOrderController {
    private final IMtDeviceOrderService mtDeviceOrderService;
    private final WeChatMiniService weChatMiniService;

    /**
     * @Description: 获取我的订单列表
     * @Param: CashWithdrawalAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "获取我的订单列表", notes = "获取我的订单列表", tags = {SwaggerConstants.API_DAIFUKUAN, SwaggerConstants.API_USERORDER, SwaggerConstants.API_MYORDER})
    @GetMapping
    public R<IPage<ApiOrderVo>> getOrderInfo(PageParam page, ApiOrderFromDto obj) {
        return R.ok(mtDeviceOrderService.getApiPageList(page, obj));
    }

    /**
     * @Description: 4G开始使用
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "4G开始使用", notes = "4G开始使用", tags = SwaggerConstants.API_HOME)
    @PostMapping("/save4gStartObj")
    public R<Boolean> save4gStartObj(@Valid @RequestBody ApiLyOrderAddDto obj) {
        return R.ok(mtDeviceOrderService.save4gStartObj(obj));
    }

    /**
     * @Description: 4G轮训接口
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "4G轮训接口", notes = "4G轮训接口", tags = SwaggerConstants.API_HOME)
    @ApiImplicitParam(value = "设备通道标识", name = "lockPassId", dataType = "Long", required = true)
    @GetMapping("/get4gObj/{lockPassId}")
    public R<Integer> get4gObj(@PathVariable("lockPassId") Long lockPassId) {
        return R.ok(mtDeviceOrderService.get4gObj(lockPassId));
    }
    /**
     * @Description: 4G轮训接口为0的处理(网络中断)(待优化)
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "4G轮训接口为0的处理(网络中断)(待优化)", notes = "4G轮训接口为0的处理(网络中断)(待优化)", tags = SwaggerConstants.API_HOME)
    @ApiImplicitParam(value = "设备通道标识", name = "lockPassId", dataType = "Long", required = true)
    @PutMapping("/get4gZeroObj/{lockPassId}")
    public R<Boolean> get4gZeroObj(@PathVariable("lockPassId") Long lockPassId) {
        return R.ok(mtDeviceOrderService.get4gZeroObj(lockPassId));
    }

    /**
     * @Description: 4G确认使用
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "4G确认使用(硬件)", notes = "4G确认使用(硬件)", tags = SwaggerConstants.API_HOME)
    @PostMapping("/save4gObj")
    @ApiIgnore
    public R<Boolean> save4gObj(@Valid @RequestBody ApiGOrderAddDto obj) {
        return R.ok(mtDeviceOrderService.save4gObj(obj));
    }

    /**
     * @Description: 蓝牙开始使用
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "蓝牙开始使用", notes = "蓝牙开始使用", tags = SwaggerConstants.API_HOME)
    @PostMapping("/saveLyObj")
    public R saveLyObj(@Valid @RequestBody ApiLyOrderAddDto obj) {
        return R.ok(mtDeviceOrderService.saveLyObj(obj));
    }

    /**
     * @Description: 设备订单支付
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "设备订单支付", notes = "设备订单支付", tags = SwaggerConstants.API_HOME)
    @ApiImplicitParam(value = "设备订单ID", name = "id", dataType = "Long", required = true)
    @PutMapping("/payOrder/{id}")
    @Limit(name = "设备订单支付频繁", prefixKey = "PAYORDER:RELEASE", period = 1, count = 1)
    public R payOrder(@PathVariable("id") Long id) {
        //下单
        ApiOrderPrepayVo orderPrepayVo = mtDeviceOrderService.payOrder(id);
        //小程序
        return weChatMiniService.callOrderPrepayment(orderPrepayVo);

    }

    /**
     * @Description: 设备订单完成
     * @Param: OrderBasketAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "设备订单完成(归还设备生成订单)", notes = "设备订单完成(归还设备生成订单)", tags = SwaggerConstants.API_HOME)
    @PutMapping("/endOrder")
    @Inner
    public R<Boolean> endOrder(@Valid @RequestBody ApiEndOrderAddDto obj) {
        return R.ok(mtDeviceOrderService.endOrder(obj));

    }

    /**
     * 小程序微信 Order订单 - 支付回调
     *
     * @param
     * @return
     */
    @Inner
    @ApiIgnore
    @PostMapping("/wxPay/minicallbackOrder")
    public String wxMiniPayLowerOrderCallback(@RequestBody String xmlData) {
        if (mtDeviceOrderService.wxMiniPayLowerOrderCallback(xmlData)) {
            return WxPayNotifyResponse.success("成功");
        }
        return WxPayNotifyResponse.fail("失败");
    }

}
